package com.kingsoft.miot.bridge.config;

import java.util.HashMap;
import java.util.Map;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kingsoft.miot.bridge.utils.RedisUtil;

/**
 * reids config 单机版 配置
 * 
 * @author HUJIANQING
 *
 */

@EnableCaching
@Configuration
//@PropertySource("classpath:redis.properties")
public class RedisConfig {

	/**
	 * 缓存实体
	 */

	public static final String HOTEL_CACHE = "_hotel_info_cahce_";
	public static final String HOUSE_CACHE = "_house_info_cahce_";
	public static final String DEVICE_CACHE = "_device_info_cahce_";
	public static final String MANAGER_CACHE = "_manager_info_cahce_";
	public static final String GROUP_CACHE = "_manager_info_cahce_";
	public static final String ACTION_GROUP_RECORD_CACHE = "_action_group_record_cache_";

	/**
	 * 集群相关
	 * 
	 * @return
	 */
//	@Value("${spring.redis.cluster.nodes}")
//	private String clusterNodes;
//
//	@Value("${spring.redis.cluster.max-redirects}")
//	private Integer mmaxRedirectsac;

	/**
	 * jedis Config
	 */
//	@Bean
//	public JedisPoolConfig jedisPoolConfig() {
//		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//		// 最大空闲数
//		jedisPoolConfig.setMaxIdle(maxIdle);
//		// 连接池的最大数据库连接数
//		jedisPoolConfig.setMaxTotal(maxTotal);
//		// 最大建立连接等待时间
//		jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
//		// 逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
//		jedisPoolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
//		// 每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
//		jedisPoolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
//		// 逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
//		jedisPoolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
//		// 是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
//		jedisPoolConfig.setTestOnBorrow(testOnBorrow);
//		// 在空闲时检查有效性, 默认false
//		jedisPoolConfig.setTestWhileIdle(testWhileIdle);
//		return jedisPoolConfig;
//	}

	/**
	 * 单机版配置 @Title: JedisConnectionFactory @param @param
	 * jedisPoolConfig @param @return @return JedisConnectionFactory @autor
	 * lpl @date 2018年2月24日 @throws
	 */
//	@Bean
//	public JedisConnectionFactory JedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
//		JedisConnectionFactory JedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);
//		// 连接池
//		JedisConnectionFactory.setPoolConfig(jedisPoolConfig);
//		// IP地址
//		JedisConnectionFactory.setHostName("192.168.177.128");
//		// 端口号
//		JedisConnectionFactory.setPort(6379);
//		// 如果Redis设置有密码
//		// JedisConnectionFactory.setPassword(password);
//		// 客户端超时时间单位是毫秒
//		JedisConnectionFactory.setTimeout(5000);
//		return JedisConnectionFactory;
//	}

	@Bean
	public CacheManager cacheManager(RedisTemplate<String, Object> redisTemplate) {
		RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
		// 多个缓存的名称,目前只定义了一个
		// 设置缓存过期时间
		Map<String, Long> expires = new HashMap<>();
		expires.put("12h", 3600 * 12L);
		expires.put("30m", 3600 * 1L);
//        expires.put("10m", 60 * 10L);
		// 设置缓存过期时间(秒)
		rcm.setExpires(expires);
		// 设置默认缓存时间
		rcm.setDefaultExpiration(6000);
		return rcm;
	}

	/**
	 * redis template
	 * 
	 * @param factory
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@Bean
	public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
		RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
		redisTemplate.setConnectionFactory(factory);
		Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
		ObjectMapper om = new ObjectMapper();
		om.setVisibility(PropertyAccessor.ALL, com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY);
		om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		jackson2JsonRedisSerializer.setObjectMapper(om);
		StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
		// key采用String的序列化方式
		redisTemplate.setKeySerializer(stringRedisSerializer);
		// hash的key也采用String的序列化方式
		redisTemplate.setHashKeySerializer(stringRedisSerializer);
		// value序列化方式采用jackson
		redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
		// hash的value序列化方式采用jackson
		redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
		redisTemplate.afterPropertiesSet();
		return redisTemplate;
	}

	/**
	 * 获取redis实例
	 * 
	 * @param redisTemplate
	 * @return
	 */
	@Bean(value = "redisUtils")
	public RedisUtil redisUtil(RedisTemplate<String, Object> redisTemplate) {

		System.out.println("redisTemplate: " + redisTemplate);
		RedisUtil instance = RedisUtil.getInstance(redisTemplate);

		System.out.println("instance: " + instance);
		return instance;
	}

}
